/// ===========================================================================
/// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
/// KIND, WHETHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
/// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
/// PURPOSE.
/// ===========================================================================
/// 
/// Project:        MOSS Faceted Search
/// Author:         Leonid Lyublinski (leonidly@microsoft.com)
/// Company:        Microsoft Services
/// Date:           09/04/2008  Version:        2.5
///
/// ===========================================================================

using System;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
using System.Xml.Serialization;
using System.Data;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.Office.Server;
using Microsoft.Office.Server.Search.Query;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace Microsoft.SharePoint.Portal.ExtendedSearch.WebControls
{
    /// <summary>
    /// A generic class to encapuslate different search query methods
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class GenericQuery<T>: IDisposable where T : Query
    {
        #region Fields
        private T _query;
        private EventHandler _customLogic;
        private Microsoft.Office.Server.Search.WebControls.CoreResultsWebPart _coreResultsWebPart; 
        #endregion

        #region Properties
        public bool EnableStemming
        {
            get { return _query.EnableStemming; }
            set { _query.EnableStemming = value; }
        }
        public bool TrimDuplicates
        {
            get { return _query.TrimDuplicates; }
            set { _query.TrimDuplicates = _coreResultsWebPart.DuplicatesRemoved; }
        }
        public bool IgnoreAllNoiseQuery
        {
            get { return _query.IgnoreAllNoiseQuery; }
            set { _query.IgnoreAllNoiseQuery = value; }
        }
        public string Scope;

        public Query Query
        {
            get { return _query; }
        } 
        #endregion

        #region ctor
        public GenericQuery(EventHandler customLogic)
        {
            _query = (T)Activator.CreateInstance(typeof(T), ServerContext.Current);
            _customLogic = customLogic;
        }
        public GenericQuery(EventHandler customLogic, Microsoft.Office.Server.Search.WebControls.CoreResultsWebPart coreResultsWebPart):this(customLogic)
        {
            _coreResultsWebPart = coreResultsWebPart;
            SyncWithCore();
        } 
        #endregion

        #region Public methods
        public ResultTableCollection Execute(EventArgs args)
        {
            _customLogic(_query, args);
            return _query.Execute();
        } 
        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            _query.Dispose();
        }

        #endregion

        #region Private methods
        private void SyncWithCore()
        {
            if (_coreResultsWebPart != null)
            {
                _query.TrimDuplicates = _coreResultsWebPart.DuplicatesRemoved;
                _query.EnableStemming = _coreResultsWebPart.StemmingEnabled;
                _query.IgnoreAllNoiseQuery = _coreResultsWebPart.NoiseIgnored;
                if (!string.IsNullOrEmpty(_coreResultsWebPart.Scope)) 
                    Scope =_coreResultsWebPart.Scope;
            }
        } 
        #endregion
    }

}

